package com.example.demo.Algorithm;

import java.util.List;

import java.time.LocalDate;
import java.time.ZoneId;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo.Entity.Employee;
import com.example.demo.Entity.Experience;
import com.example.demo.Entity.Performance;
import com.example.demo.Repository.IExperienceRepository;
import com.example.demo.Repository.IPerformanceRepository;

/**
 * @version: V0.7
 * @author: 郑皓杰 吴嘉鑫
 * @className: AlgorithmCollection
 * @packageName: com.example.demo.Algorithm
 * @description: 这是可能会用到的算法类，用于数据分析
 * @data: 2021-07-17
 **/

@Service
public class AlgorithmCollection {

	@Autowired
	private IExperienceRepository expRepository;
	@Autowired
	private IPerformanceRepository perfRepository;
	
	public boolean isFrequentSuspended(Employee emp) {
		List<Experience> list=expRepository.findByEmpid(emp.getEmpid());
		int recordcount=list.size();
		int count=0;
		
		for(Experience exp:list) {
			int temp=(int)(exp.getSuspended().toEpochDay() - exp.getRecruited().toEpochDay());
			if(temp<30) count++;
		}
		
		float rate=count/recordcount;
		if(rate>0.3f) return true;
		return false;
	}
	
	public boolean isOverrated(Employee emp) {
		List<Experience> list=expRepository.findByEmpid(emp.getEmpid());
		int expected=emp.getExpectedsalary();
		int recordcount=list.size();
		int count=0;
		for(Experience exp:list) {
			if(exp.getSalary()<expected) count++;
		}
		float rate=count/recordcount;
		if(rate>0.6f) return true;
		return false;
	}
	
	public float Wastagerate(Integer companyid) {
		List<Experience> list=expRepository.findByCompanyID(companyid);
		int recordcount=list.size();
		int count=0;
		for(Experience exp:list) {
			int temp=(int)(exp.getSuspended().toEpochDay() - exp.getRecruited().toEpochDay());
			if(temp<30) count++;
		}
		return count/recordcount;
	}
	
	public boolean isDeclined(Employee emp) {
		Experience exp = expRepository.findByEmpidAndStillOnWork(emp.getEmpid());
		List<Performance> list = perfRepository.findByExpid(exp.getExpid());
		int recordcount=list.size();
		int count=0;
		int  all = 0;
		int  nowadays = 0;
		for(Performance temp:list) {
			all = all + temp.getRank();
			int gap = (int)(temp.getStart().toInstant().atZone(ZoneId.systemDefault()).toLocalDate().toEpochDay() - LocalDate.now().toEpochDay());
			if(gap<90) {
				nowadays = nowadays + temp.getRank();
				count++;
			}
		}
		if(all/recordcount-nowadays/count>0.5) return true;
		return false;	
	}
	
}
